#include "BrowserTreeWidget.h"
#include "delegate/CstmTreeViewDelegate.h"
#include <QTreeView>
#include <iostream>

#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

BrowserTreeWidget::BrowserTreeWidget()
{
    initNodeInfoList();
    initUi();
}



BrowserTreeWidget::BrowserTreeWidget(QWidget *parent) :
    QWidget(parent)
{
    this->setMinimumWidth(200);
    this->setMaximumWidth(300);
    this->setMinimumHeight(800);
    treeView = new QTreeView (this);
    treeView->setMinimumHeight(this->height());
    std::cout << "width = " << this->width() << std::endl;
    treeView->setMinimumWidth(this->width() - 10);
    initNodeInfoList();
    initUi();
}



BrowserTreeWidget::~BrowserTreeWidget()
{

}



void
BrowserTreeWidget::initNodeInfoList()
{
    //初始化
    m_infoList.clear();

    NodeInfo info1(FRUIT_TYPE,"香蕉");
    NodeInfo info2(FRUIT_TYPE,"芒果");
    NodeInfo info3(FRUIT_TYPE,"樱桃");

    NodeInfo info4(LANGUAGE_TYPE,"英语");
    NodeInfo info5(LANGUAGE_TYPE,"中文");
    NodeInfo info6(LANGUAGE_TYPE,"法语");

    NodeInfo info7(SPORT_TYPE,"跑步");
    NodeInfo info8(SPORT_TYPE,"竞走");
    NodeInfo info9(SPORT_TYPE,"跨栏");

    m_infoList.append(info1);
    m_infoList.append(info2);
    m_infoList.append(info3);
    m_infoList.append(info4);
    m_infoList.append(info5);
    m_infoList.append(info6);
    m_infoList.append(info7);
    m_infoList.append(info8);
    m_infoList.append(info9);
}



void
BrowserTreeWidget::initUi()
{
    m_treeview_model = new QStandardItemModel(this);
    m_treeview_model->clear();


    treeView->setModel(m_treeview_model);
    treeView->setHeaderHidden(true);
    treeView->setIndentation(0);        //
    treeView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    treeView->setItemDelegate(new CstmTreeViewDelegate(this));
    treeView->setExpandsOnDoubleClick(true);

    for(int i = 0;i < m_infoList.count();++i)
    {
        NodeInfo info = m_infoList.at(i);

        QStandardItem *mainItem = NULL;
        QList<QStandardItem *> item_list;
        getAllItem(m_treeview_model->invisibleRootItem(),item_list);

        foreach (QStandardItem *item, item_list)
        {
            if(isMainItem(item->index()) && info.getNodeType() == (NodeType)item->data(nodetype_role).toInt())
            {
                mainItem = item;
                break;
            }
        }

        if(!mainItem)
        {
            mainItem = new QStandardItem(getNodeTypeStr(info.getNodeType()));
            mainItem->setData(info.getNodeType(),nodetype_role);
            //mainItem->setSizeHint(QSize(400,100));
            mainItem->setCheckable(true);
            m_treeview_model->appendRow(mainItem);  //主节点添加到模型中
        }

        QStandardItem *childItem = NULL;
        for(int row = 0; row < mainItem->rowCount();++row)
        {
            if(mainItem->child(row)->index().data().toString() == info.getNodeName())
            {
                childItem = mainItem->child(row);
                break;
            }
        }
        if(!childItem)  //添加子节点
        {
            childItem = new QStandardItem(info.getNodeName());
            childItem->setData(info.getNodeType(),nodetype_role);
            //childItem->setSizeHint(QSize(400,100));
            childItem->setCheckable(true);

            mainItem->appendRow(childItem);
        }

        treeView->sortByColumn(0,Qt::AscendingOrder);
        treeView->expandAll();
    }
}



void
BrowserTreeWidget::getAllItem(
    QStandardItem *item,
    QList<QStandardItem *> &item_list)
{
    if(!item)
        return;

    for(int i = 0;i < item->rowCount();++i)
    {
        item_list.append(item->child(i));

        getAllItem(item->child(i),item_list);
    }
}



bool
BrowserTreeWidget::isMainItem(
    const QModelIndex &index)
{
    return !index.parent().isValid();
}



void
BrowserTreeWidget::on_treeView_clicked(
    const QModelIndex &index)
{
    if(isMainItem(index))   //是mainitem
    {
        if(m_treeview_model->itemFromIndex(index)->checkState() == Qt::Unchecked)
        {
            m_treeview_model->itemFromIndex(index)->setCheckState(Qt::Checked);
            treeView->update(index);

            QList<QStandardItem *> allItems;
            allItems.clear();
            getAllItem(m_treeview_model->invisibleRootItem(),allItems);

            for(int i = 0;i < allItems.count();++i)
            {
                QStandardItem *item = allItems.at(i);
                if(isMainItem(item->index()))
                {
                    if(item->index() != index)
                    {
                        item->setCheckState(Qt::Unchecked);
                    }else
                    {
                        item->setCheckState(Qt::Checked);
                    }
                    treeView->update(item->index());
                }else
                {
                    if(item->parent()->index() != index)
                    {
                        item->setCheckState(Qt::Unchecked);
                        treeView->update(item->index());
                    }else
                    {
                        item->setCheckState(Qt::Checked);
                        treeView->update(item->index());
                    }
                }
            }

        }else
        {
            m_treeview_model->itemFromIndex(index)->setCheckState(Qt::Unchecked);
            treeView->update(index);

            QList<QStandardItem *> allItems;
            allItems.clear();
            getAllItem(m_treeview_model->itemFromIndex(index),allItems);

            for(int i = 0;i < allItems.count();++i)
            {
                QStandardItem *item = allItems.at(i);
                item->setCheckState(Qt::Unchecked);
                treeView->update(item->index());
            }
        }
    }else
    {
        QList<QStandardItem *> allItems;
        int selectedSum = 0;
        allItems.clear();
        getAllItem(m_treeview_model->invisibleRootItem(),allItems);

        if(m_treeview_model->itemFromIndex(index)->checkState() == Qt::Unchecked)
        {
            m_treeview_model->itemFromIndex(index)->setCheckState(Qt::Checked);
            treeView->update(index);

            for(int i = 0; i < allItems.count();++i)
            {
                QStandardItem *item = allItems.at(i);
                if(isMainItem(item->index()))    //其他父节点的选中状态变为未选中状态
                {
                    if(item->index() != index.parent())
                        if(item->checkState())
                        {
                            item->setCheckState(Qt::Unchecked);
                            treeView->update(item->index());
                        }
                }
                else
                {
                    if(item->index().parent() != index.parent())   //其他父节点的节点状态为未选中
                    {
                        item->setCheckState(Qt::Unchecked);
                        treeView->update(item->index());
                    }else if(item->checkState() == Qt::Checked)
                    {
                        ++selectedSum;
                    }
                }
            }
        }
        else
        {
            m_treeview_model->itemFromIndex(index)->setCheckState(Qt::Unchecked);
            treeView->update(index);

            for(int i = 0; i < allItems.count();++i)
            {
                QStandardItem *item = allItems.at(i);

                if(!isMainItem(item->index()) && item->parent()->index() == index.parent() && item->checkState() == Qt::Checked)
                {
                    ++selectedSum;
                }
            }
        }

        if(selectedSum == m_treeview_model->itemFromIndex(index)->parent()->rowCount())
        {
            m_treeview_model->itemFromIndex(index)->parent()->setCheckState(Qt::Checked);
        }else if(selectedSum == 0)
        {
            m_treeview_model->itemFromIndex(index)->parent()->setCheckState(Qt::Unchecked);
        }else
            m_treeview_model->itemFromIndex(index)->parent()->setCheckState(Qt::PartiallyChecked);

        treeView->update(index.parent());
    }

}
